package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * Author：汤小洋
 * Date：2023-07-21 16:31
 * Description：<描述>
 */
@RestController
@RequiredArgsConstructor
public class RedisController {

    private final RedisTemplate<String,Object> redisTemplate;
    // private final StringRedisTemplate redisTemplate;

    @RequestMapping("/test1")
    public String test1(){
        // System.out.println(redisTemplate.getConnectionFactory().getConnection());

        /**
         * 操作String
         */
        // redisTemplate.opsForValue().set("name","汤小洋");
        // redisTemplate.opsForValue().set("name","汤小洋",3, TimeUnit.MINUTES);
        redisTemplate.opsForValue().setIfAbsent("name","帅哥",3, TimeUnit.MINUTES); // 当key不存在时才设置

        System.out.println(redisTemplate.opsForValue().get("name"));

        /**
         * 操作List
         */
        // redisTemplate.opsForList()
        // redisTemplate.opsForSet()
        // redisTemplate.opsForZSet()
        // redisTemplate.opsForHash()

        /**
         * 存储对象
         */
        User user = new User(1,"汤小洋","123456");
        redisTemplate.opsForValue().set("user",user);

        User u  = (User) redisTemplate.opsForValue().get("user");
        System.out.println(u);

        return "success";
    }
}
